package com.example.wyemail.util;

import com.example.wyemail.handler.exception.ExceptionHandler;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Screen;

/**
 * 图标工具类
 *
 * @author fxab
 * @date 2024/06/06
 */
public class IconUtil {
    /**
     * 最大尝试次数
     */
    private static final Integer maxAttempts = 5;

    /**
     * 用于判断图像的是否存在，避免偶然性误差，每次识别间隔较长，并非用于业务判断
     *
     * @param s       s
     * @param imgPath img路径
     * @return {@link Boolean}
     * @throws ImageNotFoundException 未找到图像异常
     */
    public static Boolean IsExist(Screen s, String imgPath) throws ImageNotFoundException {
        for (int attempt = 0; attempt < maxAttempts; attempt++) {
            if (s.exists(imgPath, 1) != null) {  // 每次尝试等待 1 秒
                return true;
            }
            try {
                Thread.sleep(1000);  // 等待 1 秒后再次尝试
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();  // 恢复中断状态
                throw new RuntimeException(e);
            }
        }
        throw new ImageNotFoundException("未找到：" + imgPath);
    }

    /**
     * 自定义异常：未找到图像异常
     *
     * @author fxab
     * @date 2024/06/07
     */
    public static class ImageNotFoundException extends Exception {
        public ImageNotFoundException(String message) {
            super(message);
        }
    }
}